package com.muyizhuo.dao;

import com.muyizhuo.bean.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class StudentTest01 {

    /*
        查询全部
    */
    @Test
    public void selectAll() throws IOException {
        //1.加载核心配置文件

        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

        //2.获取SqlSession工厂对象

        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);

        //3.通过SqlSession工厂对象获取SqlSession对象

        SqlSession sqlSession = build.openSession();

        //4.执行映射文件中的sql语句并接受结果

        List<Student> list = sqlSession.selectList("StudentMapper.selectAll");

        //5.处理结果

        for (Student stu : list) {
            System.out.println(stu);
        }

        //6.释放资源

        sqlSession.close();
        is.close();
    }
}
